//
//  MyShoppingCarCell.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/29.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "MyShoppingCarCell.h"
#import "UIView+CHView.h"
@implementation MyShoppingCarCell{
    NSInteger number;
    NSInteger priceValue;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _pictureImageview = [[UIImageView alloc] init];
        [self.contentView addSubview:_pictureImageview];
        [_pictureImageview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(scaleLeftAndRight(10));
            make.top.equalTo(self.contentView.mas_top).offset(scaleTopAndBottom(2));
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(100);
        }];
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"五彩玛丽颜料color";
        _nameLabel.textColor = COLOR_HEX(0x121212);
        _nameLabel.font = [UIFont systemFontOfSize:14.0f];
        [self.contentView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_pictureImageview.mas_right).offset(scaleLeftAndRight(5));
            make.height.mas_equalTo(scaleTopAndBottom(20));
            make.right.equalTo(self.contentView.mas_right);
            make.top.equalTo(self.contentView.mas_top).offset(scaleTopAndBottom(10));
        }];
        UILabel *choosePriceLable = [[UILabel alloc] init];
        choosePriceLable.text = @"选择商品的价格";
        choosePriceLable.textColor = COLOR_HEX(0xABABAB);
        choosePriceLable.font = [UIFont systemFontOfSize:12.0f];
        [self.contentView addSubview:choosePriceLable];
        [choosePriceLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_pictureImageview.mas_right).offset(scaleLeftAndRight(5));
            make.top.equalTo(_nameLabel.mas_bottom).offset(scaleTopAndBottom(10));
            make.right.equalTo(self.contentView.mas_right).offset(-10);
            make.height.mas_equalTo(scaleTopAndBottom(15));
        }];
        _priceLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_priceLabel];
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_pictureImageview.mas_right).offset(scaleLeftAndRight(5));
            make.height.mas_equalTo(scaleTopAndBottom(20));
            make.width.mas_equalTo(scaleLeftAndRight(150));
            make.top.equalTo(choosePriceLable.mas_bottom).offset(scaleTopAndBottom(13));
//            make.bottom.equalTo(self.contentView.mas_bottom).offset(scaleTopAndBottom(-10));
        }];
        _bgview = [[UIView alloc] init];
        [self.contentView addSubview:_bgview];
        [_bgview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(scaleTopAndBottom(20));
            make.left.equalTo(_priceLabel.mas_right);
            make.bottom.equalTo(self.contentView.mas_bottom).offset(scaleTopAndBottom(-10));
            make.right.equalTo(self.contentView.mas_right);
        }];
        self.numberLabel = [[UILabel alloc] init];
        self.numberLabel.textAlignment = NSTextAlignmentCenter;
        self.numberLabel.backgroundColor = [UIColor redColor];
        [_bgview addSubview:self.numberLabel];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_bgview.mas_centerX);
            make.top.and.bottom.equalTo(_bgview);
            make.width.equalTo(_bgview.mas_width).multipliedBy(0.3);
        }];
        self.plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.plusBtn setTitle:@"+" forState:UIControlStateNormal];
        [self.plusBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.plusBtn addTarget:self action:@selector(plusTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_bgview addSubview:self.plusBtn];
        [self.plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.numberLabel.mas_right);
            make.top.equalTo(_bgview.mas_top);
            make.bottom.equalTo(_bgview.mas_bottom);
            make.right.equalTo(_bgview.mas_right).offset(-10);
        }];
        self.minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.minusBtn setTitle:@"-" forState:UIControlStateNormal];
        [self.minusBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.minusBtn addTarget:self action:@selector(minusTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_bgview addSubview:self.minusBtn];
        [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.numberLabel.mas_left);
            make.top.equalTo(_bgview.mas_top);
            make.bottom.equalTo(_bgview.mas_bottom);
            make.left.equalTo(_bgview.mas_left);
            //            make.width.mas_equalTo()
            make.width.mas_equalTo(self.plusBtn);
        }];
        self.numberLabel.layer.borderWidth = 1;
        self.numberLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.minusBtn.layer.borderWidth = 1;
        self.minusBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.plusBtn.layer.borderWidth = 1;
        self.plusBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.plusBtn.backgroundColor = [UIColor greenColor];
        self.minusBtn.backgroundColor = [UIColor greenColor];
        _pictureImageview.backgroundColor = [UIColor yellowColor];
        self.priceLabel.text = @"15";
        self.nameLabel.text = @"商品名称";
        _bgview.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)plusTapped:(UIButton *)btn {
    if (number <= 9999) {
        number++;
        [self updateNumberLabel];
        CGPoint btnPoint = [self convertPoint:self.bgview.center toView:self.superview];
        self.plusTapHandle(btnPoint);
    }
    
}

- (void)minusTapped:(UIButton *)btn {
    if (number) {
        number--;
        [self updateNumberLabel];
        self.minusTapHandle();
    }
    
}

- (void)updateNumberLabel {
    self.numberLabel.text = [NSString stringWithFormat:@"%zd", number];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
